package com.smartlink.action;

import java.util.Map;
import java.util.Random;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.service.AdminService;

public class AdminAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;
	private AdminService adminService;
	private String email;
	private String password;
	private String paramCode;
	private Map<String, Object> session;

	public String login() {
		return "SUCCESS";
	}

	public String changePassword() {
		return "SUCCESS";
	}

	public String check() {
		boolean flag = adminService.login(email, password);
		if (flag) {
			return "SUCCESS";
		} else {
			this.addFieldError("login", "Email或密码错误");
			return "FAIL";
		}

	}

	public String loginByCode() {
		int sessionCode = (int) session.get("identifiCode");
		if (paramCode.equals(String.valueOf(sessionCode))) {
			return "SUCCESS";
		} else {
			this.addFieldError("login", "验证码错误");
			return "FAIL";
		}
	}

	public void sendIdentifiCode() {
		// 生成4位验证码
		Random random = new Random();
		int identifiCode = random.nextInt(10000);
		// 发送电子邮件
		boolean flag = adminService.sendEmail(email, identifiCode);
		// 保存到session中
		if (flag) {
			session.put("identifiCode", identifiCode);
		} else {
			;
		}

	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session = arg0;

	}

	public String getParamCode() {
		return paramCode;
	}

	public void setParamCode(String paramCode) {
		this.paramCode = paramCode;
	}

}
